<?php
namespace app\admin\controller\system;
use app\admin\model\publicuse\PublicUse;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean;
use think\Request;

/**
 * @ControllerAnnotation(title="应用管理")
 * Class Myapp
 * @package app\admin\controller\system
 */
class Myapp extends AdminController
{
    /**
     * @NodeAnotation(title="列表")
     */
    public function index(){
        if($this->request->isAjax()){
            $array = PublicUse::getdir(base_path(),['admin','common']);
            $newdata = [];
            foreach ($array as $key=>$value){
                $da = base_path().$value.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json';
                if(!is_file($da)){
                    continue;
                }

                $value = json_decode(file_get_contents($da),true);
                $newdata[] = $value;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => count($newdata),
                'data'  => $newdata,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="增加")
     */
    public function add(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            $array = [
                'name|应用名'=>'require',
                'identification|应用标识'=>'require',
                'authorname|作者的名称'=>'require',
            ];
            $this->validate($data,$array);
            $yiyou = PublicUse::getdir(base_path(),['admin','common']);
            $yiyou[] = 'admin';
            $yiyou[] = 'common';
            if(in_array($data['identification'],$yiyou)){
                return $this->error('应用已存在了');
            }
            $newdata = [
                'name'=>$data['name'],
                'identification'=>$data['identification'],
                'description'=>$data['description']??'',
                'author'=>[
                    'name'=>$data['authorname'],
                    'email'=>$data['authoremail']??'',
                ],
                'prefix'=>$data['prefix']??getDataBaseConfig('prefix'),
                'install'=>0,
            ];
            if(!is_dir(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install')){
                mkdir(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install',0777,true);
            }
            chmod(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install',0777);
            $bool = file_put_contents(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json',json_encode($newdata,JSON_UNESCAPED_UNICODE));
            if(!$bool){
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="修改")
     */
    public function edit(){
        $data = $this->request->param();
//        echo '<pre>';
//        print_r($data);
//        exit;
        if($this->request->isAjax()){
            $array = [
                'name|应用名'=>'require',
                'identification|应用标识'=>'require',
                'authorname|作者的名称'=>'require',
            ];
            $this->validate($data,$array);
            $newdata = [
                'name'=>$data['name'],
                'identification'=>$data['identification'],
                'description'=>$data['description']??'',
                'author'=>[
                    'name'=>$data['authorname'],
                    'email'=>$data['authoremail']??'',
                ],
                'prefix'=>$data['prefix']??getDataBaseConfig('prefix'),
                'install'=>0,
            ];
            if(is_file(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json')){
                $jshuju = json_decode(file_get_contents(base_path().$data['app'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json'),true);
                $newdata['install'] = $jshuju['install']??0;
            }
            if(!is_file(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json')){
                return $this->error('应用配置文件不存在');
            }
            if(!is_dir(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install')){
                mkdir(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install',0777,true);
            }
            chmod(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install',0777);
            $bool = file_put_contents(base_path().$data['identification'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json',json_encode($newdata,JSON_UNESCAPED_UNICODE));
            if(!$bool){
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        }
        if(empty($data['app'])){
            return $this->error('应用不存在');
        }
        if(!is_file(base_path().$data['app'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json')){
            return $this->error('应用配置文件不存在');
        }
        $shuju = json_decode(file_get_contents(base_path().$data['app'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json'),true);
        $this->assign('row',$shuju);
        return $this->fetch();
    }
    public function modify(){
        return $this->error('这里不能使用');
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete(){
        $app = $this->request->param('app','');
        if(empty($app)){
            return $this->error('请输入要删除的应用');
        }
        if(!is_dir(base_path().$app)){
            return $this->error('应用不存在');
        }
        $bool = PublicUse::deleteDir(base_path().$app);
        if(!$bool){
            return $this->error('删除失败');
        }
        return $this->success('删除成功');
    }
    /**
     * @NodeAnotation(title="安装")
     */
    public function install(Request $request){
        $data = $request->param();
        if(empty($data['app'])){
            return $this->error('请输入app的标识');
        }
        $admin_dir = base_path().$data['app'].DIRECTORY_SEPARATOR.'admin';
        $bool = true;
        if(is_dir($admin_dir)){
            $obj = new PublicUse();
            $bool = $obj->install($data['app']);
        }
        if(!$bool){
            return $this->error($obj->getMessage());
        }
        $config_dir = base_path().$data['app'].DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR.'config.json';
        if(!is_file($config_dir)){
            $newdata = [
                'name'=>'',
                'identification'=>$data['app'],
                'author'=>[
                    'name'=>'',
                    'email'=>'',
                ],
                'install'=>1
            ];
            if(!is_dir(base_path().$data['app'].DIRECTORY_SEPARATOR.'install')){
                mkdir(base_path().$data['app'].DIRECTORY_SEPARATOR.'install',0777,true);
            }
            chmod(base_path().$data['app'].DIRECTORY_SEPARATOR.'install',0777);
            file_put_contents($config_dir,json_encode($newdata,JSON_UNESCAPED_UNICODE));
        }
        $newdata = json_decode(file_get_contents($config_dir),true);
        $newdata['install'] = 1;
        $newdata['prefix'] = getDataBaseConfig('prefix');
//        echo '<pre>';
//        print_r($newdata);
//        exit;
        chmod(base_path().$data['app'].DIRECTORY_SEPARATOR.'install',0777);
        $bool = file_put_contents($config_dir,json_encode($newdata,JSON_UNESCAPED_UNICODE));
        if(!$bool){
            return $this->error('保存配置失败');
        }
        return $this->success('安装成功');
    }
    /**
     * @NodeAnotation(title="卸载")
     */
    public function uninstall($app){

    }
    /**
     * @NodeAnotation(title="导出")
     */
    public function export($app){
//        echo '<pre>';
//        print_r($app);
//        exit;
    }
}